package com.molichuxing.services.infrastructure.service.impl;

import com.molichuxing.services.infrastructure.convertor.Convert;
import com.molichuxing.services.infrastructure.dao.EnterpriseCarHandoverMapper;
import com.molichuxing.services.infrastructure.dto.request.create.EnterpriseCarHandoverCreateDto;
import com.molichuxing.services.infrastructure.dto.response.EnterpriseCarHandoverDto;
import com.molichuxing.services.infrastructure.service.EnterpriseCarHandoverService;
import com.molichuxing.services.property.EnterpriseCarHandoverTypeEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import com.molichuxing.services.infrastructure.bean.EnterpriseCarHandoverBean;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 企业订单车辆交接
 *
 * @Author zoumingyu
 * @Date 2020年06月08日
 */
@Service("enterpriseCarHandoverService")
public class EnterpriseCarHandoverServiceImpl implements EnterpriseCarHandoverService {
    private static final Logger logger = LoggerFactory.getLogger(OrderEnterpriseServiceImpl.class);

    @Resource
    private EnterpriseCarHandoverMapper enterpriseCarHandoverMapper;

    /**
     * 获取企业订单车辆交接信息
     *
     * @param orderCode 订单号
     * @param type      交接类型
     * @return
     */
    @Override
    public List<EnterpriseCarHandoverDto> getEnterpriseCarHandover(Long orderCode,
                                                                   EnterpriseCarHandoverTypeEnum type) {
        return Convert.toEnterpriseCarHandoverDto(enterpriseCarHandoverMapper.getEnterpriseCarHandoverByOrderCode(orderCode,
                type == null ? null : type.getValue()));
    }

    /**
     * 获取企业订单车辆交车信息
     *
     * @param orderCode 订单号
     * @return
     */
    @Override
    public List<EnterpriseCarHandoverDto> getEnterpriseCarDelivery(Long orderCode) {
        return getEnterpriseCarHandover(orderCode, EnterpriseCarHandoverTypeEnum.DELIVERY);
    }

    /**
     * 获取企业订单车辆还车（退租）信息
     *
     * @param orderCode 订单号
     * @return
     */
    @Override
    public List<EnterpriseCarHandoverDto> getEnterpriseCarReturn(Long orderCode) {
        return getEnterpriseCarHandover(orderCode, EnterpriseCarHandoverTypeEnum.RETURN);
    }

    /**
     * 车辆交接添加
     *
     * @param enterpriseCarHandoverCreateDto
     * @return
     */
    @Override
    public Boolean createCarhandover(EnterpriseCarHandoverCreateDto enterpriseCarHandoverCreateDto) {
        EnterpriseCarHandoverBean enterpriseCarHandoverBean = Convert.toEnterpriseCarHandoverBean(enterpriseCarHandoverCreateDto);
        enterpriseCarHandoverMapper.create(enterpriseCarHandoverBean);
        return true;
    }

    /**
     * 交车信息
     *
     * @param id
     * @return
     */
    @Override
    public EnterpriseCarHandoverDto getById(Integer id) {
        EnterpriseCarHandoverBean enterpriseCarHandoverBean = enterpriseCarHandoverMapper.getById(id);
        return Convert.toEnterpriseCarHandoverDto(enterpriseCarHandoverBean);
    }

    /**
     * 根据退租时间查询
     * @param params
     * @return
     */
    @Override
    public List<EnterpriseCarHandoverDto> getByReturnedTime(Map<String, Object> params) {
        List<EnterpriseCarHandoverDto> res = new ArrayList<>();
        List<EnterpriseCarHandoverBean> list= enterpriseCarHandoverMapper.getByReturnedTime(params);
        if(list!=null && list.size()>0){
            for (EnterpriseCarHandoverBean bean : list) {
                if(bean!=null ){
                    EnterpriseCarHandoverDto dto = Convert.toEnterpriseCarHandoverDto(bean);
                    res.add(dto);
                }
            }
        }
        return res;
    }

    @Override
    public Map<String, EnterpriseCarHandoverDto> getByDto(List<EnterpriseCarHandoverDto> carHandoverDtoList, EnterpriseCarHandoverTypeEnum delivery) {
        if(carHandoverDtoList==null ||carHandoverDtoList.size()<1){
            return null;
        }
        Map<String,EnterpriseCarHandoverDto> res = new HashMap<>();
        List<EnterpriseCarHandoverBean> list = enterpriseCarHandoverMapper.getByDto(carHandoverDtoList,delivery.getValue());
        if(list!=null && list.size()>0){
            for (EnterpriseCarHandoverBean bean : list) {
                if(bean!=null ){
                    EnterpriseCarHandoverDto dto = Convert.toEnterpriseCarHandoverDto(bean);
                    res.put(bean.getOrderCode()+"_"+bean.getCarId(),dto);
                }
            }
        }
        return res;
    }


}
